using BuildingBlocks.Abstractions.Permissions;
using Identity.Application.PermissionKeyResourceIds.Interfaces;
using Microsoft.EntityFrameworkCore;

namespace Identity.Infrastructure.Persistence.ReadServices;

public class ApiPermissionBindingReadService : IApiPermissionBindingReadService
{
    private readonly ApplicationDbContext _context;
    public ApiPermissionBindingReadService(ApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<IReadOnlyCollection<PermissionBindingDto>> GetBindingsAsync(string? serviceName, CancellationToken ct = default)
    {
        if (string.IsNullOrWhiteSpace(serviceName))
        {
            return await _context.ApiPermissionBindings
            .AsNoTracking()
            .Select(p => new PermissionBindingDto
            {
                permissionKey = p.Permission.Key,
                resourceId = p.ResourceId,
                isActive = p.IsActive,
                description = p.Description,
                fromServiceName = p.FromServiceName,
                PermissionScope = p.PermissionScope.Value
            })
            .ToListAsync(ct);
        }
        else
        {
            return await _context.ApiPermissionBindings
            .AsNoTracking()
           .Where(p => p.FromServiceName == serviceName)
           .Select(p => new PermissionBindingDto
           {
               permissionKey = p.Permission.Key,
                resourceId = p.ResourceId,
                isActive = p.IsActive,
                description = p.Description,
                fromServiceName = p.FromServiceName,
                PermissionScope = p.PermissionScope.Value
           })
           .ToListAsync(ct);
        }

    }

    public async Task<IReadOnlyCollection<PermissionBindingDto>> GetBindingsByPermissionKeyAsync(string permissionKey, CancellationToken ct = default)
    {
        return await _context.ApiPermissionBindings
            .AsNoTracking()
            .Where(p => p.Permission.Key == permissionKey)
            .Select(p => new PermissionBindingDto
            {
                permissionKey = p.Permission.Key,
                resourceId = p.ResourceId
            })
            .ToListAsync(ct);
    }

    public async Task<PermissionBindingDto?> GetBindingsByResourceIdAsync(string resourceId, CancellationToken ct = default)
    {
        var binding = await _context.ApiPermissionBindings
            .AsNoTracking()
            .Where(p => p.ResourceId == resourceId)
            .Include(per => per.Permission)
            .Select(p => new PermissionBindingDto
            {
                permissionKey = p.Permission.Key,
                resourceId = p.ResourceId
            }).FirstOrDefaultAsync(ct);
        if (binding == null) return null;

        return binding; 
    }

    public Task<bool> IsUserCenterPermissionAsync(string permissionKey)
    {
        return _context.ApiPermissionBindings.Where(p => p.Permission.Key == permissionKey).FirstOrDefault()?.PermissionScope.Value == "Centralized"
            ? Task.FromResult(true)
            : Task.FromResult(false);
    }
}
